#include "config.h"

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */

#include <stdlib.h>
#include <libgen.h>
#include <stdio.h>

#include <cmark/defs.h>

static void show_version(void);
static void show_usage(const char * proc);

int
main(int argc, char * argv[])
{
	char * proc;
	int    ch;

	proc = basename(argv[0]);

	loop {
		ch = getopt(argc, argv, "hv");
		if (ch < 0)
			break;

		switch (ch) {
			case 'v':
				show_version();
				return EXIT_SUCCESS;

			case 'h':
				show_usage(proc);
				return EXIT_SUCCESS;

			default:
				return EXIT_FAILURE;
		}
	}

	argc -= optind;
	argv += optind;

	return EXIT_SUCCESS;
}

static void
show_version(void)
{
	fprintf(
		stderr,
		"C* compiler, version %d.%d.%d (%s/%s).\n"
		"Copyright (c) 2012, %s.\n\n",
		CMARK_VERSION_MAJOR,
		CMARK_VERSION_MINOR,
		CMARK_VERSION_PATCH,
		BUILD_SYSTEM,
		BUILD_PROCESSOR,
		CMARK_VENDOR);
}

static void
show_usage(const char * proc)
{
	show_version();

	fprintf(
		stderr,
		"Usage:\n"
		"  %s [options]\n\n"
		"Options:\n"
		"  -h  Print this help and exit\n"
		"  -v  Print version information and exit\n",
		proc);
}
